package de.hska.algo.labor2010.strecken;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;


public class LineTest {

	private Point p1;
	private Point p2;
	private Line line;
	private Line normalIntersect;
	private Line parallel;
	private Line same;
	private Line noIntersect;
	private Line xLine;
	private Line yLine;
	private Line yShortLine;
	private Line xShortLine;

	@Before
	public void initialize() {
		p1 = new Point(1.0, 1.0);
		p2 = new Point(5.0, 5.0);
		line = new Line(p1, p2);
		normalIntersect = new Line(1.0, 5.0, 5.0, 1.0);
		noIntersect = new Line(1.0, 5.0, 2.0, 4.0);
		parallel = new Line(1.0, 2.0, 5.0, 6.0);
		same = new Line(1.0, 1.0, 5.0, 5.0);
		xLine = new Line(1.0, 1.0, 5.0, 1.0);
		yLine = new Line(2.0, 0.0, 2.0, 5.0);
		yShortLine = new Line(2.0, 2.0, 2.0, 5.0);
		xShortLine = new Line(3.0, 1.0, 5.0, 1.0);
		
	}



	@Test
	public void testLinePointPoint() {
		Line aLine = new Line(p1, p2);
		Assert.assertEquals(1.0, aLine.getP1().getX(), 1.9e-16);
		Assert.assertEquals(1.0, aLine.getP1().getY(), 1.9e-16);
		Assert.assertEquals(5.0, aLine.getP2().getX(), 1.9e-16);
		Assert.assertEquals(5.0, aLine.getP2().getY(), 1.9e-16);
		
		
	}
	@Test(expected = IllegalArgumentException.class)
	public void testLinePointPointNull1(){
		new Line(null, new Point(1.0,1.0));
	}
	@Test(expected = IllegalArgumentException.class)
	public void testLinePointPointNull2(){
		new Line( new Point(1.0,1.0),null);
	}
	@Test(expected = IllegalArgumentException.class)
	public void testLinePointPointNull3(){
		new Line(null, null);
	}

	@Test
	public void testLineDoubleDoubleDoubleDouble() {
		Line aLine = new Line(1.0, 1.0, 5.0, 5.0);
		Assert.assertEquals(1.0, aLine.getP1().getX(), 1.9e-16);
		Assert.assertEquals(1.0, aLine.getP1().getY(), 1.9e-16);
		Assert.assertEquals(5.0, aLine.getP2().getX(), 1.9e-16);
		Assert.assertEquals(5.0, aLine.getP2().getY(), 1.9e-16);
	}

	

	@Test
	public void testIntersectSimple() {
		Point intersection1 = line.intersect(normalIntersect);
		Assert.assertEquals(3.0, intersection1.getX(), 1.9e-16);
		Assert.assertEquals(3.0, intersection1.getY(), 1.9e-16);
		Point intersection2 = xLine.intersect(yLine);
		Assert.assertEquals(2.0, intersection2.getX(), 1.9e-16);
		Assert.assertEquals(1.0, intersection2.getY(), 1.9e-16);
		Point intersection3 = xLine.intersect(yLine);
		Assert.assertEquals(2.0, intersection3.getX(), 1.9e-16);
		Assert.assertEquals(1.0, intersection3.getY(), 1.9e-16);
		
//		
//		Point intersection = line.intersectSimple(normalLine);
//		Assert.assertEquals(3.0, intersection.getX(), 1.9e-16);
//		Assert.assertEquals(3.0, intersection.getY(), 1.9e-16);
//		Point intersection2 = x.intersectSimple(y);
//		Assert.assertEquals(2.0, intersection2.getX(), 1.9e-16);
//		Assert.assertEquals(1.0, intersection2.getY(), 1.9e-16);
		
		
	}

	@Test
	public void testGetM() {
		Assert.assertEquals(1.0, line.getM(), 1.9e-16);
		Assert.assertEquals(0.0, xLine.getM(), 1.9e-16);
		
	}

	@Test
	public void testGetC() {
		Assert.assertEquals(0.0, line.getC(), 1.9e-16);
	}


	@Test
	public void testIntersect() {
		
		Point intersection1 = line.intersect(normalIntersect);
		Assert.assertEquals(3.0, intersection1.getX(), 1.9e-16);
		Assert.assertEquals(3.0, intersection1.getY(), 1.9e-16);
		Point intersection2 = xLine.intersect(yLine);
		Assert.assertEquals(2.0, intersection2.getX(), 1.9e-16);
		Assert.assertEquals(1.0, intersection2.getY(), 1.9e-16);
		Point intersection3 = xLine.intersect(yLine);
		Assert.assertEquals(2.0, intersection3.getX(), 1.9e-16);
		Assert.assertEquals(1.0, intersection3.getY(), 1.9e-16);
	}

	@Test
	public void testIsIntersectSimple() {
		Assert.assertTrue(line.isIntersect(normalIntersect));
		Assert.assertTrue(normalIntersect.isIntersect(line));
		Assert.assertTrue(xLine.isIntersect(yLine));
		Assert.assertTrue(yLine.isIntersect(xLine));
		Assert.assertTrue(xLine.isIntersect(line));
		Assert.assertTrue(yLine.isIntersect(line));
		Assert.assertTrue(line.isIntersect(xLine));
		Assert.assertTrue(line.isIntersect(yLine));
		
		
		Assert.assertFalse(line.isIntersect(same)); 
		Assert.assertFalse(line.isIntersect(line));
		Assert.assertFalse(line.isIntersect(parallel));
		Assert.assertFalse(line.isIntersect(noIntersect));
		Assert.assertFalse(yLine.isIntersect(xShortLine));
		Assert.assertFalse(yShortLine.isIntersect(xLine));
		Assert.assertFalse(xLine.isIntersect(yShortLine));

	}

	@Test
	public void testIsIntersect() {
		Assert.assertTrue(line.isIntersect(normalIntersect));
		Assert.assertTrue(xLine.isIntersect(yLine));
		Assert.assertTrue(yLine.isIntersect(xLine));
		
		Assert.assertFalse(line.isIntersect(same)); 
		Assert.assertFalse(line.isIntersect(line));
		Assert.assertFalse(line.isIntersect(parallel));
		Assert.assertFalse(line.isIntersect(noIntersect));
		Assert.assertFalse(noIntersect.isIntersect(line));
		Assert.assertFalse(xLine.isIntersect(yShortLine));
		Assert.assertFalse(yLine.isIntersect(xShortLine));
		Assert.assertFalse(xShortLine.isIntersect(yLine));
	}
	/*
	 * private boolean isinYRange(Line someLine, double x) {
		double possibleY = someLine.getM() * x + someLine.getC();
		return ((possibleY <= p1.getY() && possibleY >= p2.getY()) || (possibleY <= p2.getY()
				&& possibleY >= p1.getY()));
	}
	 */
	
	

	@Test
	public void testGetP1() {
		Assert.assertEquals(1.0, line.getP1().getX(), 1.9e-16);
		Assert.assertEquals(1.0, line.getP1().getY(), 1.9e-16);
	}

	

	@Test
	public void testGetP2() {
		Assert.assertEquals(5.0, line.getP2().getX(), 1.9e-16);
		Assert.assertEquals(5.0, line.getP2().getY(), 1.9e-16);
	}

	
}
